﻿using System;
using System.Diagnostics;
using System.Reflection;

namespace ZHello.Utils
{
    public class TraceLog
    {
        private static TraceListener _listener;

        static TraceLog()
        {
            var name = Assembly.GetExecutingAssembly().GetName().Name;
            _listener = new DefaultTraceListener()
            {
                Name = "CodeLog",
                LogFileName = $"{name}.log",
                TraceOutputOptions = TraceOptions.ProcessId | TraceOptions.ThreadId | TraceOptions.DateTime | TraceOptions.Timestamp
            | TraceOptions.Callstack,
            };
        }

        public static void InitLog(bool enable)
        {
            if (enable)
            {
                if (Trace.Listeners.Contains(_listener))
                    Trace.Listeners.Add(_listener);
            }
            else
            {
                if (Trace.Listeners.Contains(_listener))
                    Trace.Listeners.Remove(_listener);
            }
        }

        public static void WriteLine(string msg)
        {
            Trace.WriteLine(msg);
        }

        public static void WriteLine(object obj)
        {
            Trace.WriteLine(obj);
        }

        public static void Error(Exception ex)
        {
            Trace.TraceError(null, ex);
        }

        public static void Warn(Exception ex)
        {
            Trace.TraceWarning(null, ex);
        }

        public static void Info(Exception ex)
        {
            Trace.TraceInformation(null, ex);
        }

        public static void Error(string msg, Exception ex = null)
        {
            Trace.TraceError(msg, ex);
        }

        public static void Warn(string msg, Exception ex = null)
        {
            Trace.TraceWarning(msg, ex);
        }

        public static void Info(string msg, Exception ex = null)
        {
            Trace.TraceInformation(msg, ex);
        }
    }
}